
package com.application.chiettinhdienluc.ui;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.application.chiettinhdienluc.R;
import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.data.GlobalData;
import com.application.chiettinhdienluc.data.SharedPreferencesData;
import com.application.chiettinhdienluc.db.DonViQuanLyDataSource;
import com.application.chiettinhdienluc.db.MySQLiteHelper;
import com.application.chiettinhdienluc.db.NhanVienDataSource;
import com.application.chiettinhdienluc.model.DonViQuanLyModel;
import com.application.chiettinhdienluc.model.NguoiDungModel;
import com.application.chiettinhdienluc.utils.DeviceState;
import com.application.chiettinhdienluc.utils.Utils;

public class LoginActivity extends SherlockActivity {

	private final String TAG = LoginActivity.class.getSimpleName();

	Resources res;

	private Button btn_login;

	private boolean is_new = false;

	@SuppressLint("SdCardPath")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.login_layout);
		// copyDB();
		getWidhHeight();
		res = getResources();

		getSupportActionBar().setTitle(res.getString(R.string.str_title_LoginAct));
		
		TextView tv_ten_dvql = (TextView) findViewById(R.id.tv_ten_dvql);
		DonViQuanLyDataSource dvDataSource = new DonViQuanLyDataSource(MainApplication.getAppContext());
		
		DonViQuanLyModel donvi = dvDataSource.getDonViByMaDonVi(SharedPreferencesData.getMaDonViQuanLy(MainApplication.getAppContext()));
		
		if(donvi!=null) {
		    String tenDonViQuanLy = donvi.getTenDonVi();
		    tv_ten_dvql.setText(tenDonViQuanLy);
		    MainApplication.setMaKhuVuc(donvi.getKhuVuc());
		    
		    Log.i("Ma Don Vi",  SharedPreferencesData.getMaDonViQuanLy(MainApplication.getAppContext()));
		    
		   
		    
		    Log.i("Ma Khu Vuc from DB",  donvi.getKhuVuc());
		    Log.i("Ma Khu Vuc", MainApplication.getMaKhuVuc());
		    
		}
			
		
		//Toast.makeText(MainApplication.getAppContext(), DeviceState.getDeviceId(), Toast.LENGTH_SHORT).show();

		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				NhanVienDataSource ndDataSource = new NhanVienDataSource();

				EditText edt_username = (EditText) findViewById(R.id.edt_username);
				EditText edt_password = (EditText) findViewById(R.id.edt_password);
				CheckBox chk_connection = (CheckBox) findViewById(R.id.chk_local_connection);

				NguoiDungModel nd = ndDataSource.getLoginInfo(edt_username.getText().toString().trim(), edt_password.getText().toString().trim());

				if (nd != null) {

					MainApplication.setNguoiDung(nd);
					MainApplication.setLocalConnect(chk_connection.isChecked());
					
					if(TextUtils.isEmpty(SharedPreferencesData.getMaDonViQuanLy(MainApplication.getAppContext()))) 
//					|| TextUtils.isEmpty(SharedPreferencesData.getMaKhuVuc(MainApplication.getAppContext())))
					{
						Intent localIntent = new Intent(LoginActivity.this, com.application.chiettinhdienluc.ui.SettingsActivity.class);
						localIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						localIntent.putExtra(SettingsActivity.BOOL_EXTRA_START_FROM_LOGIN, true);
						startActivity(localIntent);
					}
					else{
						Intent localIntent = new Intent(LoginActivity.this, com.application.chiettinhdienluc.ui.DanhMucHsoActivity.class);
						localIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(localIntent);
					}					
					finish();
				} else {
					String error_message = res.getString(R.string.str_LoginError);
					edt_username.setError(error_message);
					Utils.showMess(error_message);
				}
			}
		});
	}

	@SuppressWarnings("deprecation")
	private void getWidhHeight() {
		Display display = getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		int height = display.getHeight();
		GlobalData.widthDevice = width;
		GlobalData.heightDevice = height;

	}
}